package w20rest.java;

import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.support.RestGatewaySupport;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import sun.net.www.http.HttpClient;

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;

public class ConditionGetClient {

    public static final String AP = "http://localhost:8888/";

    @Test
    public void test() {

        testGetAllCustomers();

    }

    private void testGetAllCustomers() {
        String uri = AP;
        uri += "customers";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        final UriComponents encode = uriComponentsBuilder.build().encode();
        final String uriString = encode.toUriString();
        System.out.println(uriString);

        final HashMap<String, String> map = new HashMap<String, String>();
//        map.put("If-None-Match", "a1ff139edbbb91b9e463533a1482242c");
        map.put("If-Modified-Since", "Sat Mar 23 2013 16:19:22 GMT+0100 (Central Europe Standard Time)");

        final RestTemplate restTemplate = RestUtil.createRestTemplate(map);
//        String response = restTemplate.getForObject(uriString, String.class);

        final ResponseExtractor<String> responseExtractor = new ResponseExtractor<String>() {

            public String extractData(ClientHttpResponse response) throws IOException {
                final HttpHeaders headers = response.getHeaders();
                System.out.println(response.getStatusCode() + " " + response.getStatusText());
                System.out.println(headers);

                return null;  //To change body of implemented methods use File | Settings | File Templates.
            }
        };

        final RequestCallback requestCallback = new RequestCallback() {
            public void doWithRequest(ClientHttpRequest request) throws IOException {
                final HttpHeaders headers = request.getHeaders();
                System.out.println(headers);
            }
        };

        String s = restTemplate.execute(uriString, HttpMethod.GET, requestCallback, responseExtractor);

//        new HttpClient(encode.toUri(),)


//        System.out.println(response);
//        response = restTemplate.getForObject(uriString, String.class);
//        System.out.println(response);

    }


}
